<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>商品管理</cite></a>
    <a><cite>转赠记录</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layadmin-search">
      <form class="layui-form" id="filterForm" lay-filter="form_give_search">
        <script type="text/html" template lay-done="layui.data.done()">
          <div class="layui-inline">
            <div class="layui-form-label">关键字搜索</div>
            <div class="layui-input-inline" style="width: 200px">
              <input type="text" name="keyword" value="{{decodeURIComponent(layui.router().search.keyword || '')}}"
                     class="layui-input"
                     placeholder="产品名称">
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-form-label">日期范围</div>
            <div class="layui-inline" id="date_range">
              <div class="layui-input-inline">
                <input type="text" name="start_date" autocomplete="off" id="start_date" class="layui-input"
                       style="width: 100px" value="{{layui.router().search.start_date || ''}}" placeholder="开始日期">
              </div>
              <div class="layui-input-inline">~</div>
              <div class="layui-input-inline">
                <input type="text" name="end_date" autocomplete="off" id="end_date" class="layui-input"
                       style="width: 100px" value="{{layui.router().search.end_date || ''}}" placeholder="结束日期">
              </div>
            </div>
          </div>
          <div class="layui-inline layui-btn-group">
            <button type="submit" lay-submit class="layui-btn" lay-filter="submit_give_search">
              <i class="layui-icon layui-icon-search"></i>查询
            </button>
            <button type="submit" lay-submit class="layui-btn layui-btn-warm"
                    lay-filter="reset_give_search">
              <i class="layui-icon layui-icon-close"></i>重置
            </button>
            <button type="submit" lay-submit class="layui-btn layui-btn-info"
                    lay-filter="give_export">
              <i class="layui-icon layui-icon-export"></i>导出
            </button>
          </div>
        </script>
      </form>
    </div>
    <div class="layui-card-body">
      <div id="table_give"></div>
      <script type="text/html" id="tpl_give_user">
        {{ d.give_user.user.mobile }}
      </script>
      <script type="text/html" id="tpl_receive_user">
        {{ d.user.mobile }}
      </script>
      <script type="text/html" id="tpl_product_id">
        {{ d.product.id ?? '' }}
      </script>
      <script type="text/html" id="tpl_product_name">
        {{ d.product.name ?? '' }}
      </script>
      <script type="text/html" id="tpl_status">
        <span class="layui-font-green">转赠成功</span>
      </script>
    </div>
  </div>
</div>

<script>
  layui.data.done = function () {
    layui.use(['fox_table','admin', 'laydate'], function () {
      let $ = layui.$
        , fox_table = layui.fox_table
        , form = layui.form
        , admin = layui.admin
        , laydate = layui.laydate

      //日期时间选择器
      laydate.render({
        elem: '#date_range'
        , range: ['#start_date', '#end_date']
        , max: (new Date()).toDateString()
      });
      form.render()

      let resourceUrl = 'agent-admin/asset/give_records'

      // 列表查询
      fox_table.render({
        id: 'table_give'
        , url: resourceUrl
        , cols: [[
          {title: 'ID', field: 'id', width: 80, align: "center", sort: true}
          , {title: '商品ID', templet: '#tpl_product_id', width: 100}
          , {title: '产品名称', templet: '#tpl_product_name', minWidth: 160}
          , {title: '转赠人', templet: '#tpl_give_user', width: 250}
          , {title: '接收人', templet: '#tpl_receive_user', width: 250}
          , {title: '转赠时间', field: 'created_at', width: 180, align: "center"}
          , {title: '转赠状态', templet: '#tpl_status', width: 100}
        ]]
      });

      form.on('submit(give_export)', function (data) {
        layer.load();
        admin.post('agent-admin/asset/give_records/export' , data.field,function (res) {
          layer.confirm('数据下载中，请到下载中心查看下载任务进度.', function (index) {
            layer.close(index);
            location.hash = '/ops/download';
            return false;
          });
          layer.closeAll('loading');
        }, {hasLoading: true})
        return false;
      });

      //搜索
      form.on('submit(submit_give_search)', function (d) {
        fox_table.reload(d.field);
        return false;
      });
      //重置
      form.on('submit(reset_give_search)', function (d) {
        $("input[name='keyword']").val('');
        $("input[name='start_date']").val('');
        $("input[name='end_date']").val('');
        form.render()
        fox_table.reload({});
        return false;
      });
    })
  }
</script>

